// import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ToastController, LoadingController, Loading, AlertController } from 'ionic-angular';
import { Storage } from '@ionic/storage';



/*
  Generated class for the PublicCompoentsProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class PublicCompoentsProvider {
    private loading: Loading;
    private loadingIsOpen: boolean = false;

    constructor( 
        public toastCtrl: ToastController, 
        public loadingCtrl: LoadingController,
        public alertCtrl: AlertController,
        public storage: Storage,
         ) {
        console.log('Hello PublicCompoentsProvider Provider');
    }

    /**
     * toast 提示框
     * @param msg 提示的消息
     * @param position 弹框位置
     */ 
    public toast( msg, position ) {
        const toast = this.toastCtrl.create({
            message: msg,
            duration: 1000,
            position: position,
            cssClass: 'toastCss'
        });
        toast.onDidDismiss(() => {
            console.log('Dismissed toast');
        });
        toast.present();
    };

    /**
     * 统一调用此方法显示loading
     * @param content 显示的内容
     */
    showLoading(content: string = ''): void {
        if (!this.loadingIsOpen) {
            this.loadingIsOpen = true;
            this.loading = this.loadingCtrl.create({
                content: content
            });
            this.loading.present();
            setTimeout(() => {
                //最长显示10秒
                this.loadingIsOpen && this.loading.dismiss();
                this.loadingIsOpen = false;
            }, 50000);
        }
    };

    /**
     * 关闭loading
     */
    hideLoading(): void {
        this.loadingIsOpen && this.loading.dismiss();
        this.loadingIsOpen = false;
    };

    /**
     * alert 弹框
     * @param _title 提示词
     * @param _msg 提示内容
     * @param btn 按钮词
     * @param callback 回调
     */
    showAlert( _title, _msg, btn, callback ) {
        const alert = this.alertCtrl.create({
            title: _title,
            subTitle: _msg,
            buttons: [
                {
                    text: btn,
                    handler: () => {
                        callback;
                    }
                },
            ]
        });
        alert.present();
    }

    doubleAlert() {
        let alert = this.alertCtrl.create({
            title: '提示',
            message: '确认清除系统缓存?',
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                        console.log('Cancel clicked');
                    }
                }, {
                    text: '确认',
                    handler: () => {
                        this.storage.remove('userId');
                        this.storage.remove('userInfo');
                        console.log( '清除成功！' )
                        setTimeout(()=>{
                            window.location.href = 'http://second.shscv.com.cn'
                        },300);
                    }
                }
            ]
        });
        alert.present();
    }

}
